﻿using System;
using System.Collections.Generic;
using System.Text;
using Microsoft.Practices.EnterpriseLibrary.Data;
using Microsoft.Practices.EnterpriseLibrary.Data.Sql;
using System.Data.Common;
using System.Data;


namespace Chenxin.Lib.House.DAL
{
    public class DataDictionaryItem
    {
        public DataDictionaryItem() {}

        #region  成员方法
        /// <summary>
        /// 是否存在该记录
        /// </summary>
        public static bool Exists(Guid RecordId)
        {
            Database db = DatabaseFactory.CreateDatabase("Chenxin_House");
            StringBuilder strSql = new StringBuilder();
            strSql.Append("select count(1) from DataDictionaryItem where ItemKey=@ItemKey ");
            DbCommand dbCommand = db.GetSqlStringCommand(strSql.ToString());
            db.AddInParameter(dbCommand, "ItemKey", DbType.String, RecordId);
            int cmdresult;
            object obj = db.ExecuteScalar(dbCommand);
            if ((Object.Equals(obj, null)) || (Object.Equals(obj, System.DBNull.Value)))
            {
                cmdresult = 0;
            }
            else
            {
                cmdresult = int.Parse(obj.ToString());
            }
            if (cmdresult == 0)
            {
                return false;
            }
            else
            {
                return true;
            }
        }


        /// <summary>
        /// 增加一条数据
        /// </summary>
        public static void Add(Chenxin.Lib.House.Entity.DataDictionaryItem model)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("insert into DataDictionaryItem(");
            strSql.Append("ItemKey,ItemRemark)");

            strSql.Append(" values (");
            strSql.Append("@ItemKey,@ItemRemark)");
            Database db = DatabaseFactory.CreateDatabase("Chenxin_House");
            DbCommand dbCommand = db.GetSqlStringCommand(strSql.ToString());
            db.AddInParameter(dbCommand, "ItemKey", DbType.AnsiString, model.ItemKey);
            db.AddInParameter(dbCommand, "ItemRemark", DbType.AnsiString, model.ItemRemark);
            db.ExecuteNonQuery(dbCommand);
        }
        /// <summary>
        /// 更新一条数据
        /// </summary>
        public static void Update(Chenxin.Lib.House.Entity.DataDictionaryItem model)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("update DataDictionaryItem set ");
            strSql.Append("ItemRemark=@ItemRemark,");
            strSql.Append(" where ItemKey=@ItemKey ");
            Database db = DatabaseFactory.CreateDatabase("Chenxin_House");
            DbCommand dbCommand = db.GetSqlStringCommand(strSql.ToString());
            db.AddInParameter(dbCommand, "ItemKey", DbType.AnsiString, model.ItemKey);
            db.AddInParameter(dbCommand, "ItemRemark", DbType.AnsiString, model.ItemRemark);
            db.ExecuteNonQuery(dbCommand);

        }

        /// <summary>
        /// 删除一条数据
        /// </summary>
        public static void Delete(string ItemKey)
        {

            StringBuilder strSql = new StringBuilder();
            strSql.Append("delete from DataDictionaryItem ");
            strSql.Append(" where ItemKey=@ItemKey ");
            Database db = DatabaseFactory.CreateDatabase("Chenxin_House");
            DbCommand dbCommand = db.GetSqlStringCommand(strSql.ToString());
            db.AddInParameter(dbCommand, "ItemKey", DbType.AnsiString, ItemKey);
            db.ExecuteNonQuery(dbCommand);

        }

        /// <summary>
        /// 得到一个对象实体
        /// </summary>
        public static Chenxin.Lib.House.Entity.DataDictionaryItem GetModel(string ItemKey)
        {

            StringBuilder strSql = new StringBuilder();
            strSql.Append("select ItemKey,ItemRemark from DataDictionaryItem ");
            strSql.Append(" where ItemKey=@ItemKey ");
            Database db = DatabaseFactory.CreateDatabase("Chenxin_House_Search");
            DbCommand dbCommand = db.GetSqlStringCommand(strSql.ToString());
            db.AddInParameter(dbCommand, "ItemKey", DbType.AnsiString, ItemKey);
            Entity.DataDictionaryItem model = null;
            using (IDataReader dataReader = db.ExecuteReader(dbCommand))
            {
                if (dataReader.Read())
                {
                    model = ReaderBind(dataReader);
                }

                dataReader.Close();
                dataReader.Dispose();
            }
            return model;
        }

        /// <summary>
        /// 获得数据列表
        /// </summary>
        public static DataSet GetList(string strWhere, string strOrder)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("select ItemKey,ItemRemark ");
            strSql.Append(" FROM DataDictionaryItem ");
            if (strWhere.Trim() != "")
            {
                strSql.Append(" where " + strWhere);
            }
            Database db = DatabaseFactory.CreateDatabase("Chenxin_House_Search");
            return db.ExecuteDataSet(CommandType.Text, strSql.ToString());
        }

        /*
        /// <summary>
        /// 分页获取数据列表
        /// </summary>
        public DataSet GetList(int PageSize,int PageIndex,string strWhere)
        {
            Database db = DatabaseFactory.CreateDatabase();
            DbCommand dbCommand = db.GetStoredProcCommand("UP_GetRecordByPage");
            db.AddInParameter(dbCommand, "tblName", DbType.AnsiString, "DataDictionary");
            db.AddInParameter(dbCommand, "fldName", DbType.AnsiString, "ID");
            db.AddInParameter(dbCommand, "PageSize", DbType.Int32, PageSize);
            db.AddInParameter(dbCommand, "PageIndex", DbType.Int32, PageIndex);
            db.AddInParameter(dbCommand, "IsReCount", DbType.Boolean, 0);
            db.AddInParameter(dbCommand, "OrderType", DbType.Boolean, 0);
            db.AddInParameter(dbCommand, "strWhere", DbType.AnsiString, strWhere);
            return db.ExecuteDataSet(dbCommand);
        }*/

        /// <summary>
        /// 获得数据列表（比DataSet效率高，推荐使用）
        /// </summary>
        /// <param name="strWhere">where 条件</param>
        /// <param name="strOrder">排序条件</param>
        /// <returns></returns>
        public static List<Chenxin.Lib.House.Entity.DataDictionaryItem> GetListArray(string strWhere, string strOrder)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("select ItemKey,ItemRemark ");
            strSql.Append(" FROM DataDictionaryItem ");
            if (strWhere.Trim() != "")
            {
                strSql.Append(" where " + strWhere);
            }

            if (strOrder.Trim() != "")
            {
                strSql.Append(" Order By " + strOrder);
            }

            List<Entity.DataDictionaryItem> list = new List<Entity.DataDictionaryItem>();
            Database db = DatabaseFactory.CreateDatabase("Chenxin_House_Search");
            using (IDataReader dataReader = db.ExecuteReader(CommandType.Text, strSql.ToString()))
            {
                while (dataReader.Read())
                {
                    list.Add(ReaderBind(dataReader));
                }

                dataReader.Close();
                dataReader.Dispose();
            }
            return list;
        }


        /// <summary>
        /// 对象实体绑定数据
        /// </summary>
        public static Entity.DataDictionaryItem ReaderBind(IDataReader dataReader)
        {
            Entity.DataDictionaryItem model = new Entity.DataDictionaryItem();

            model.ItemKey = dataReader["ItemKey"].ToString();
            model.ItemRemark = dataReader["ItemRemark"].ToString();
            return model;
        }

        #endregion  成员方法
    }
}
